package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.BillMapper;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Bill;

import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;



    @Override
    public PageResult getBilllist(PageResult pageResult) {
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(flag, "name", pageResult.getQuery());
        IPage<Bill> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        page = billMapper.selectPage(page, queryWrapper);
        //获取总数
        long total = page.getTotal();
        //获取记录数
        List<Bill> rows = page.getRecords();
        //将数据封装
        return pageResult.setTotal(total).setRows(rows);
    }

    @Transactional
    @Override
    public void deleteBillById(Integer id) {
        billMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void addBill(Bill bill) {
        bill.setStatus(bill.getPrice()* bill.getNum()).setCreated(new Date());
        billMapper.insert(bill);
    }


    @Transactional
    @Override
    public void updateBill(Bill bill) {
        billMapper.updateById(bill);
    }

    @Override
    public Bill getBillById(Integer id) {
        return billMapper.selectById(id);
    }


}
